<template>
   <!-- 权限管理 -->
   <div class="dashboard-container">
      <div class="app-container">
         <PageTools>
            <el-button
               slot="after"
               type="primary"
               @click="addPermission(1, '0')"
            >
               添加权限
            </el-button>
         </PageTools>
         <el-table :data="list" border row-key="id">
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="标识" prop="code"></el-table-column>
            <el-table-column
               label="描述"
               prop="description"
            ></el-table-column>
            <el-table-column label="操作">
               <template slot-scope="{ row }">
                  <el-button
                     type="text"
                     @click="addPermission(2, row.id)"
                     v-if="row.type === 1"
                  >
                     添加
                  </el-button>
                  <el-button @click="editPermission" type="text">
                     编辑
                  </el-button>
                  <el-button @click="delPermission(row.id)" type="text">
                     删除
                  </el-button>
               </template>
            </el-table-column>
         </el-table>
         <!-- 新增和编辑的弹出层 -->
         <el-dialog
            :visible="showDialog"
            :title="showText"
            @close="btnCancel"
         >
            <el-form
               label-width="120px"
               :model="formData"
               :rules="rules"
               ref="perForm"
            >
               <el-form-item label="名称" prop="name">
                  <el-input v-model="formData.name"></el-input>
               </el-form-item>
               <el-form-item label="标识" prop="code">
                  <el-input v-model="formData.code"></el-input>
               </el-form-item>
               <el-form-item label="描述">
                  <el-input v-model="formData.description"></el-input>
               </el-form-item>
               <el-form-item label="开启">
                  <el-switch
                     active-value="1"
                     inactive-value="0"
                  ></el-switch>
               </el-form-item>
            </el-form>
            <!-- 确定取消 -->
            <el-row type="flex" justify="center" slot="footer">
               <el-col :span="6">
                  <el-button size="small" type="primary" @click="btnOK">
                     确定
                  </el-button>
                  <el-button size="small" @click="btnCancel">
                     取消
                  </el-button>
               </el-col>
            </el-row>
         </el-dialog>
      </div>
   </div>
</template>

<script>
import {
   getPermissionList,
   delPermission,
   addPermission,
   getPermissionDetail
   // updatePermission
} from '../../api/primission'
import { tranListToTreeData } from '../../utils'
// 引入转换为树形结构
export default {
   data() {
      return {
         // 所有权限点
         list: [],
         showDialog: false,
         formData: {
            name: '', // 名称
            code: '', // 标识
            description: '', // 描述
            type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
            pid: '', // 因为做的是树 需要知道添加到哪个节点下了
            enVisible: '0' // 开启
         },
         rules: {
            name: [
               {
                  required: true,
                  trigger: 'blur',
                  message: '名称不能为空'
               }
            ],
            code: [
               {
                  required: true,
                  trigger: 'blur',
                  message: '标识不能为空'
               }
            ]
         },
         userId: ''
      }
   },
   created() {
      this.getPermissionList()
   },
   computed: {
      showText() {
         return this.formData.id ? '编辑权限' : '新增权限'
      }
   },
   methods: {
      // 获取所有权限点
      async getPermissionList() {
         this.list = tranListToTreeData(await getPermissionList(), '0')
      },
      // 删除
      async delPermission(id) {
         try {
            await this.$confirm('确定删除该权限点吗？')
            await delPermission(id)
            this.$message.success('删除成功')
            this.getPermissionList()
         } catch (error) {
            console.log(error)
         }
         this.userId = id
      },
      // 添加
      addPermission(type, pid) {
         this.formData.type = type
         this.formData.pid = pid
         this.showDialog = true
      },
      // 确定
      async btnOK() {
         try {
            await this.$refs.perForm.validate()
            if (this.formData.id) {
               // 编辑
               await await getPermissionDetail(this.userId)
            } else {
               await addPermission(this.formData)
            }
            await addPermission(this.formData)
            this.$message.success('操作成功')
            this.showDialog = false
         } catch (error) {
            console.log(error)
         }
      },
      // 取消
      btnCancel() {
         // 清除所有数据
         this.formData = {
            name: '', // 名称
            code: '', // 标识
            description: '', // 描述
            type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
            pid: '', // 因为做的是树 需要知道添加到哪个节点下了
            enVisible: '0' // 开启
         }
         // 清除校验
         this.$refs.perForm.resetFields()
         this.showDialog = false
      },
      // 编辑
      async editPermission() {
         // this.formData = await getPermissionDetail(id)
         this.showDialog = true
      }
   }
}
</script>

<style></style>
